package cn.myschool.web;

import cn.myschool.pojo.Page;
import cn.myschool.pojo.Subject2;
import cn.myschool.service.SubjectService;
import cn.myschool.util.StrUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebServlet("/subjectServlet")
@WebInitParam(name = "user",value = "root")
public class SubjectServlet extends HttpServlet {
    //    @Override
    //    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        super.service(req, resp);
    //    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //long start=System.nanoTime();
        String subname= StrUtil.toStr(req.getParameter("subjectname"));

        int pageNo= StrUtil.toInt(req.getParameter("pageNo"),1);
        if(pageNo<1){
            resp.sendError(401,"分页条件错误");
            return;
        }
        Page<Subject2> p = SubjectService.ME.paging(pageNo, subname);

        req.setAttribute("subname",subname);
        req.setAttribute("p",p);

        req.getRequestDispatcher("/WEB-INF/jsp/subject.jsp").forward(req,resp);
//        long end=System.nanoTime();
//        System.out.println("花费时间是"+(end-start)+"纳秒");
    }

    @Override
    public void init() throws ServletException {
        ServletConfig config = super.getServletConfig();

    }
}
